package com.example.bleuesprit.parkAndroidClient;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.example.bleuesprit.parkAndroidClient.net.NetUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by bleuesprit on 2015/11/5.
 */
public class SearchParkByNameActivity extends Activity  {


    private ArrayList<HashMap<String,String>> showParksName;
    private ListView parkListView;
    private EditText searchEdit;
    private Button searchBtn;
    private JSONArray receiveParksJA;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0x123) {

                try {
                    JSONObject jsonObject = new JSONObject(msg.getData().getString("result"));



                    int reciveParksCount = Integer.valueOf(jsonObject.getString("total"));
                    if(reciveParksCount==0)
                    {
                        Toast.makeText(SearchParkByNameActivity.this, jsonObject.getString("没有任何停车场"), Toast.LENGTH_SHORT).show();
                    }
                    ArrayList<MarkerOptions> markerOptionlst = new ArrayList<MarkerOptions>();
                    markerOptionlst.ensureCapacity(reciveParksCount);
                    receiveParksJA = new JSONArray(jsonObject.getString("parkData"));



                    showParksName=new ArrayList<HashMap<String,String>>();

                    for(int i=0;i<reciveParksCount;i++)
                    {
                        JSONObject temp = receiveParksJA.getJSONObject(i);
                        HashMap<String,String> tempPark=new HashMap<String,String>();
                        tempPark.put("name",temp.getString("name"));
                        showParksName.add(tempPark);
                    }

                    parkListView.setAdapter(
                            new SimpleAdapter(SearchParkByNameActivity.this,showParksName,android.R.layout.simple_list_item_1,
                                    new String[]{"name"},
                                    new int[]{android.R.id.text1}
                            )
                    );

                    parkListView.setOnItemClickListener(
                            new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent toParkDetailIntent = new Intent(SearchParkByNameActivity.this, ParkDetailActivity.class);

                                    try {
                                        toParkDetailIntent.putExtra("parkDetailJsonString", receiveParksJA.get(position).toString());
                                    } catch (JSONException e) {
                                        Log.e("SearchParkByName", "put parkdetailString into toParkDetailIntent fail");
                                        e.printStackTrace();
                                    }

                                    startActivity(toParkDetailIntent);
                                }
                            }
                    );

                }catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }

    };








    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_park);
        parkListView=(ListView)findViewById(R.id.searchpark_parksListView);
        searchBtn=(Button)findViewById(R.id.searchpark_searchBtn);
        searchEdit=(EditText)findViewById(R.id.searchpark_searchEdit);

        searchBtn.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        searchByName();
                    }
                }
        );
    }


    private void searchByName()
    {

        JSONObject searchParkingMessage=new JSONObject();
        try {
            searchParkingMessage.put("name", searchEdit.getText().toString());

        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtil.sendToServer("searchParksByName", searchParkingMessage, handler);
    }



}